package com.badger.spring.cloud.alibaba.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @EnableDiscoveryClient 开启服务注册和发现
 */
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication2 {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(RibbonConsumerApplication2.class, args);
    }

    @Bean
    @LoadBalanced // Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具。
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    public class DemoController {
        @Autowired
        private RestTemplate restTemplate;

        private static final String REST_URL_PREFIX = "http://badger-spring-cloud-alibaba-provider";

        @GetMapping("/ribbon/demo")
        public String getAddress() {
            return restTemplate.getForObject(REST_URL_PREFIX + "/demo", String.class);
        }
    }
}
